package cn.minsh.lims.eoms.modules.app.controller;

import cn.minsh.lims.eoms.common.utils.PageUtils;
import cn.minsh.lims.eoms.common.utils.ParamUtils;
import cn.minsh.lims.eoms.common.utils.R;
import cn.minsh.lims.eoms.modules.app.controller.req.UserInspectionPlanReq;
import cn.minsh.lims.eoms.modules.app.controller.res.EquipmentStatusRes;
import cn.minsh.lims.eoms.modules.app.controller.res.UserInspectionPlanInfoForPlatRes;
import cn.minsh.lims.eoms.modules.app.controller.res.UserInspectionPlanRes;
import cn.minsh.lims.eoms.modules.app.entity.UserInspectionPlanEntity;
import cn.minsh.lims.eoms.modules.app.entity.UserInspectionPlanScopeEntity;
import cn.minsh.lims.eoms.modules.app.service.UserInspectionPlanService;
import cn.minsh.lims.eoms.modules.app.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


/**
 * 用户巡检计划
 *
 * @author lht
 * @email sunlightcs@gmail.com
 * @date 2021-04-26 02:30:42
 */
@RestController
@RequestMapping("sys/userInspectionPlan")
@Api(tags = "巡检工单(优化)")
public class UserInspectionPlanController  extends AbstractController{
    @Autowired
    private UserInspectionPlanService userInspectionPlanService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("巡检工单列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "ok",response = UserInspectionPlanRes.class)
    })
    public R list(@RequestBody UserInspectionPlanReq req){
        PageUtils page = userInspectionPlanService.queryPage(req);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("巡检工单详情")
    @ApiResponses({
            @ApiResponse(code = 200,message = "ok",response = UserInspectionPlanInfoForPlatRes.class)
    })
    public R info(@PathVariable("id") Long id, HttpServletRequest request){
        UserInspectionPlanInfoForPlatRes userInspectionPlan = userInspectionPlanService.queryPlatInfoById(id);
        userInspectionPlan.setLoginUserId(UserUtils.getUserId(request));
        return R.ok().put("data", userInspectionPlan);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody @Valid UserInspectionPlanEntity userInspectionPlan, BindingResult bindingResult){
        try{
            ParamUtils.check(bindingResult);
            userInspectionPlanService.save(userInspectionPlan);
            return R.ok();
        }catch (Exception e){
            return R.error(e.getMessage()) ;
        }
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody @Valid UserInspectionPlanEntity userInspectionPlan,
                    BindingResult bindingResult){
        try {
            ParamUtils.check(bindingResult);
            userInspectionPlanService.updateById(userInspectionPlan);
            return R.ok();
        }catch (Exception e){
            return R.error(e.getMessage()) ;
        }

    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestParam("id") Long id){
        return R.ok();
    }


    @GetMapping("getEquipmentStatusList")
    @ApiOperation("设备巡检结果")
    public R getEquipmentStatusList(){
        List<EquipmentStatusRes> list = userInspectionPlanService.getEquipmentStatusList() ;
        return R.ok().put("data",list) ;
    }

}
